﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;

namespace DALLayer.Providers
{
    public class VehicleModelTypeDataProvider : DataAccessBase
    {
         #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleModelTypeDataProvider" /> class.
        /// </summary>
        public VehicleModelTypeDataProvider() { }
        #endregion

        #region [Public Methods]
        /// <summary>
        /// Get vehicle model type details by id or all vehicle model types
        /// </summary>
        /// <param name="vehicleModelTypeId">vehicle model type id</param>
        /// <param name="selectById">select by id</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetVehicleModelTypeDetails(int vehicleModelTypeId, bool selectById)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT VehicleModelType.* FROM VehicleModelType");
                //if its a selection by id
                if (selectById == true)
                {
                    //Add the sql string to retrieve data based on VehicleModelTypeID
                    base.CommandTextBuilder.Append(@" WHERE (VehicleModelTypeID = @VehicleModelTypeID)");
                    //Add the params needed to retrieve data
                    base.DBParams.Add(@"@VehicleModelTypeID", vehicleModelTypeId);
                }

                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.VehicleModelType.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        } 
        /// <summary>
        /// The method inserts vehicle model type to the table
        /// </summary>
        /// <param name="vehicleModelTypeName">vehicle model type name</param>
        /// <param name="vehicleModelID">vehicle model id</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool InsertVehicleModelType(string vehicleModelTypeName, int vehicleModelID, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    INSERT INTO VehicleModelType(VehicleModelTypeName, VehicleModelID, IsActive)
                    VALUES(@VehicleModelTypeName, @VehicleModelID, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@VehicleModelTypeName", vehicleModelTypeName);
                base.DBParams.Add(@"@VehicleModelID", vehicleModelID);
                base.DBParams.Add(@"@IsActive", isActive);
                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }
        /// <summary>
        /// The method inserts vehicle model type to the table and returns the identity
        /// </summary>
        /// <param name="vehicleModelTypeName">vehicle model type name</param>
        /// <param name="vehicleModelID">vehicle model id</param>
        /// <param name="isActive">is active</param>
        /// <returns>int</returns>
        public int InsertVehicleModelTypeScalar(string vehicleModelTypeName, int vehicleModelID, bool isActive)
        {

            int result = 0;
            try
            {

                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"
                    INSERT INTO VehicleModelType(VehicleModelTypeName, VehicleModelID, IsActive)
                    VALUES(@VehicleModelTypeName, @VehicleModelID, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@VehicleModelTypeName", vehicleModelTypeName);
                base.DBParams.Add(@"@VehicleModelID", vehicleModelID);
                base.DBParams.Add(@"@IsActive", isActive);
                //get the result                
                result = base.ExecuteScalar();
                //check if result > 0
              
                
            }
            catch (Exception)
            {
                //Log here
            }
            //return success
            return result;
        }
        /// <summary>
        /// The method updates vehicle model type to the table
        /// </summary>
        /// <param name="vehicleModelTypeID">vehicle model type ID</param>
        /// <param name="vehicleModelTypeName">vehicle model type name</param>
        /// <param name="vehicleModelID">vehicl model ID </param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool UpdateVehicleModelType(int vehicleModelTypeID, string vehicleModelTypeName, int vehicleModelID, bool isActive)
        {
            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    UPDATE VehicleModelType
                    SET VehicleModelTypeName = @VehicleModelTypeName,
                        VehicleModelID = @VehicleModelID,
                        IsActive = @IsActive                       
                        WHERE VehicleModelTypeID = @VehicleModelTypeID");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@VehicleModelTypeName", vehicleModelTypeName);
                base.DBParams.Add(@"@vehicleModelID", vehicleModelID);
                base.DBParams.Add(@"@IsActive", isActive);
                base.DBParams.Add(@"@VehicleModelTypeID", vehicleModelTypeID);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;

        }
        #endregion
    }
}
